package bme.data.datatypes {
	import bme.data.BRC_DataType;

	/**
	 * 	String start_time;
	String end_time;
	 * @author Rendall
	 */
	public class BRC_Occurrence extends BRC_DataType {
		public var start_time : String;
		public var end_time : String;
		private var _startTime : Date;
		private var _endTime : Date;

		/**
		 * Occurrence Constructor
		 *  "start_time": "2010-09-01 19:30:00", 
		 *  "end_time": "2010-09-02 06:00:00"
		 */
		public function BRC_Occurrence(data : Object = null) {
			trace("\n BRC_Occurrence.BRC_Occurrence( data:" + data + ")");
			if (data) this.compose(data);
		}

		public function get startTime() : Date {
			if (!_startTime) _startTime = parseDate(start_time);
			return _startTime;
		}	

		public function get endTime() : Date {
			if (!_endTime) _endTime = parseDate(end_time);
			return _endTime;
		}	

		private function parseDate(string_time : String) : Date {
			var dayTime : Array = string_time.split(" ");
			var yearMonthDay : Array = dayTime[0].split("-");
			var hourMinSec : Array = dayTime[1].split(":");
			
			var year : uint = parseInt(yearMonthDay[0]);
			var month : uint = parseInt(yearMonthDay[1]) - 1;
			var day : uint = parseInt(yearMonthDay[2]);
			
			var hour : uint = parseInt(hourMinSec[0]);			var min : uint = parseInt(hourMinSec[1]);			var sec : uint = parseInt(hourMinSec[2]);
			
			return new Date(year, month, day, hour, min, sec);
		}

		public function getFormattedFull(isAllDay : Boolean = false) : String {
			var startDay:String = getFormattedDate(startTime);
			var startHour:String = getFormattedHour(startTime, isAllDay);
			
			var startTimeStr : String = startDay + " " + startHour;
			if (isAllDay) return startTimeStr;
			
			var endDay:String = "";
			if (startTime.date!=endTime.date) endDay = getFormattedDate(endTime);
			
			var endHour:String = getFormattedHour(endTime);
			
			return startTimeStr + " - " + endDay + endHour;
			
		}

		private function getFormattedHour(date : Date, isAllDay : Boolean = false) : String {
			if (isAllDay) return " all day";
			if (date.hours==0 && date.minutes==0) return "midnight ";
			if (date.hours==12 && date.minutes==0) return "noon ";
			
			var isAfterNoon : Boolean = date.hours > 12;
			var returnHour : String;
			var ampm : String;
			var returnMin : String = "";
			
			if (isAfterNoon) {
				returnHour = (date.hours - 12).toString();
				ampm = " PM";
			}else {
				returnHour = date.hours.toString();
				ampm = " AM";				
			}
			
			
			if (date.minutes) {
				returnMin = ":" + date.minutes + " ";	
			}
			
			return returnHour + returnMin + ampm;
			
			
		}

		private function getFormattedDate(date : Date) : String {
			var dateStr : String = date.toString();
			var sliceAfter : int = dateStr.indexOf(" ", 8);
			
			dateStr = dateStr.slice(0, sliceAfter);
			var lastNum : String = dateStr.charAt(dateStr.length - 1);
			switch (lastNum) {
				case "1": dateStr += "st "; break;                    
				case "2": dateStr += "nd "; break;
				case "3": dateStr += "rd "; break;
				default: dateStr += "th "; 
			}
			
			return dateStr;

		}

		public function toString() : String {
			return start_time;
		}
	}
}
